package com.example.administrator.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.example.administrator.adapter.MsgContentRecyclerViewAdapter;
import com.example.administrator.base.BaseActivity;
import com.example.administrator.bean.MsgContent;
import com.example.administrator.module.UrlDefine;
import com.example.administrator.testapk.R;
import com.example.administrator.utils.NetUtils;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

public class AtyNormalMsg extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.rl_normalmessage) RecyclerView recyclerView;
    @BindView(R.id.layout_swipe_refresh) SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.normalmsg_list_title) TitleBar normalmsg_list_title;

    private static final String TAG = "AtyNormalMsg";
    private MsgContentRecyclerViewAdapter myNormalMsgRecyclerViewAdapter;
    private RecyclerView.LayoutManager mLayoutManager;


    @Override
    protected int getLayoutResourceId() {
        return R.layout.aty_normal_message;
    }

    @Override
    protected void init() {
        NetUtils.getInstance().getDataAsynFromNet(UrlDefine.ACTION_URL_SEARCHGENERAL, new NetUtils.MyNetCall() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String normalmsg = response.body().string();

                Gson gson = new Gson();
                final MsgContent msgContent = gson.fromJson(normalmsg,MsgContent.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(msgContent.getStatus() == 200){
                            final List<MsgContent.DataBean.ListBean> list = msgContent.getData().getList();

                            myNormalMsgRecyclerViewAdapter = new MsgContentRecyclerViewAdapter(AtyNormalMsg.this, (ArrayList<MsgContent.DataBean.ListBean>) list);
                            mLayoutManager = new LinearLayoutManager(AtyNormalMsg.this,
                                    LinearLayoutManager.VERTICAL, false);
                            recyclerView.setAdapter(myNormalMsgRecyclerViewAdapter);
                            recyclerView.setLayoutManager(mLayoutManager);
                            recyclerView.setItemAnimator(new DefaultItemAnimator());//默认动画
                            recyclerView.setHasFixedSize(true);//效率最高

                            myNormalMsgRecyclerViewAdapter.setOnItemClickListener(new MsgContentRecyclerViewAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("systemName",list.get(position).getSystemName());
                                    bundle.putString("account",list.get(position).getAccount());
                                    bundle.putString("pwd",list.get(position).getPassword());
                                    overlay(AtyMsgContent.class,bundle);
                                }
                            });
                        }
                    }
                });
                Log.e(TAG,normalmsg);
            }

            @Override
            public void failed(Call call, IOException e) {
                Log.e(TAG,e.getMessage());
            }
        });
    }

    @Override
    protected void initView() {
        normalmsg_list_title.setLeftImageResource(R.mipmap.back);
        normalmsg_list_title.setTitle("信息列表");
        normalmsg_list_title.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onRefresh() {
        //下拉的时候添加项目到数据源
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}